package com.rogue.employee.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rogue.employee.domain.HsDepartment;
import com.rogue.employee.domain.dto.DeptDTO;
import com.rogue.employee.domain.vo.DepartmentVo;
import com.rogue.employee.mapper.HsDepartmentMapper;
import com.rogue.employee.service.HsDepartmentService;
import java.util.Objects;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
* @author Yeah
* @description 针对表【hs_department(部门和服务表)】的数据库操作Service实现
* @createDate 2022-03-16 14:33:08
*/
@Service
public class HsDepartmentServiceImpl extends ServiceImpl<HsDepartmentMapper, HsDepartment>
    implements HsDepartmentService{

    @Resource
    private HsDepartmentMapper mapper;

    @Override
    public PageInfo<DepartmentVo> listDeptment(DeptDTO deptDTO) {

        PageInfo<DepartmentVo> pageInfo = PageHelper.startPage(deptDTO.getPage(),
                deptDTO.getCount())
            .doSelectPageInfo(() -> mapper.selectList(new LambdaQueryWrapper<HsDepartment>()
                .eq(Objects.nonNull(deptDTO.getDeptId()), HsDepartment::getId, deptDTO.getDeptId())
                .like(StrUtil.isNotBlank(deptDTO.getName()), HsDepartment::getName,
                    deptDTO.getName())
                .eq(Objects.nonNull(deptDTO.getParentId()), HsDepartment::getParentId,
                    deptDTO.getParentId())));
        pageInfo.setList(CglibUtil.copyList(pageInfo.getList(), DepartmentVo::new));
        pageInfo.getList().stream().forEach(x->{
            HsDepartment hsDepartment = mapper.selectById(x.getParentId());
            if (Objects.nonNull(hsDepartment)){
                x.setParentName(hsDepartment.getName());
            }
        });
        return pageInfo;
    }
}




